package com.xb.cloud.usercenter.client;

import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.xb.cloud.common.util.StringUtils;
import com.xb.cloud.gateway.client.Adapter;
import com.xb.cloud.gateway.client.AdapterContext;
import com.xb.cloud.gateway.client.RtnInfo;
import com.xb.cloud.usercenter.client.vo.FavoriteShopVo;
import com.xb.cloud.usercenter.service.UserFavoriteShopService;






/**
 * 
 * @ClassName: SellOrderAdapter
 * @Description:用户订单操作
 * @author: 鲜备科技
 * @date: 2018年1月4日 下午6:47:36
 * 
 * @Copyright: 2018 www.xianbeitech.com Inc. All rights reserved.
 *
 */
@Component
public class UserFavoriteShopAdapter extends Adapter {

	private final Logger logger = LoggerFactory.getLogger(UserFavoriteShopAdapter.class);

	@Resource
	UserFavoriteShopService userFavoriteShopService;

	/**
	 * 
	 * <p>
	 * Title: execute
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @param urlPath
	 * @param isLogined
	 * @param bizInfo
	 * @param context
	 * @return
	 * @see com.xb.cloud.gateway.client.Adapter#execute(java.lang.String,
	 *      java.lang.Boolean, java.util.Map,
	 *      com.xb.cloud.gateway.client.AdapterContext)
	 */
	public RtnInfo execute(String urlPath, Boolean isLogined, Map<String, String> bizInfo, AdapterContext context) {
		long time = System.currentTimeMillis();

		RtnInfo rtnInfo = null;
		long companyId = context.getCompanyId();
		long currentUserId = context.getCurrentUserId();

		FavoriteShopVo favoriteShopVo = new FavoriteShopVo();
		favoriteShopVo.setCompanyId(companyId);
		favoriteShopVo.setEndUserId(currentUserId);
		String shopId = bizInfo.get("shopId");
		if (StringUtils.isNotBlank(shopId)) {
			favoriteShopVo.setShopId(Long.valueOf(shopId));
		}

		if ("/h5/favoriteShop/add".equalsIgnoreCase(urlPath)) {
			return userFavoriteShopService.addFavoriteShop(favoriteShopVo);
		}

		if ("/h5/favoriteShop/isFollowed".equalsIgnoreCase(urlPath)) {
			return userFavoriteShopService.isFollowed(favoriteShopVo);
		}

		if ("/h5/favoriteShop/cancelFollow".equalsIgnoreCase(urlPath)) {
			return userFavoriteShopService.cancelFollow(favoriteShopVo);
		}

		if ("/h5/favoriteShop/list".equalsIgnoreCase(urlPath)) {
			// 获取用户关注店铺列表
			Integer pageNum = null;
			Integer pageSize = null;
			try {
				pageNum = Integer.parseInt(bizInfo.get("pageNum"));
				pageSize = Integer.parseInt(bizInfo.get("pageSize"));
			} catch (NumberFormatException e) {
				logger.error("非法参数错误,入参为:" + bizInfo);
				return RtnInfo.ParameterErrRtnInfo("非法参数错误");
			}

			favoriteShopVo.setPageNum(pageNum);
			favoriteShopVo.setPageSize(pageSize);
			return userFavoriteShopService.getFavoriteShopList(favoriteShopVo);
		}
		logger.debug("执行时间：{}", System.currentTimeMillis() - time);
		return rtnInfo;
	}

	@Override
	public String getUrlMappingPath() {
		return "/h5/favoriteShop/**";
	}

	@Override
	public boolean isCheckUserToken() {
		return true;
	}
}
